#    tribalgrowth is a growth monitor for the online game Tribal Wars.
#    Copyright 2009 Nathan Michaels                                            
#                                                                              
#    This file is part of tribalgrowth.                                        
#                                                                              
#    tribalgrowth is free software: you can redistribute it and/or modify      
#    it under the terms of the GNU General Public License as published by      
#    the Free Software Foundation, either version 2 of the License, or         
#    (at your option) any later version.                                       
#                                                                              
#    tribalgrowth is distributed in the hope that it will be useful,           
#    but WITHOUT ANY WARRANTY; without even the implied warranty of            
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             
#    GNU General Public License for more details.                              
#                                                                              
#    You should have received a copy of the GNU General Public License         
#    along with tribalgrowth.  If not, see <http://www.gnu.org/licenses/>. 
"""
This module is for reading the tribal wars growth database. A simple example of
how to use it is in fastest.py, which shows how to determine the village that
grew the fastest in the last hour.
"""
from pysqlite2 import dbapi2 as db

class ReadOnlyError (Exception):
    pass

class ROCursor (db.Cursor):
    def execute (self, *args, **kwargs):
        if 'SELECT' not in args[0].upper ():
            raise ReadOnlyError ("This database is in read-only mode.")
        return super (ROCursor, self).execute (*args, **kwargs)

class RODB (object):
    """
    Open database at fileName read-only. This is not intended to
    thwart malicious access, but it will try to make sure you
    don't accidentally write to the database.
    Properties:
    ids        - A list of village IDs. Handy.
    villages   - A list of all the village tables.
    deltas     - A list of all the delta tables.
    cursor     - The read-only cursor. Use it to execute sql statements
                 like "cursor.execute('SELECT * FROM village1')".
    cur        - Shortcut for "cursor"
    """
    def __init__ (self, fileName='tribal.db'):
        self._connection = db.connect (fileName)
        self.cursor = ROCursor (self._connection)

        self.cursor.execute ('SELECT id FROM villages')
        self.cur = self.cursor
        villageIds = self.cursor.fetchall()

        self.villages = []
        self.deltas = []
        self.ids = []
        for villId in villageIds:
            ID = str (villId[0])
            self.ids.append (int (ID))
            self.villages.append ('village' + ID)
            self.deltas.append ('delta' + ID)

    def __del__ (self):
        self._connection.close ()
